// 选中指定行
function selectRow(tableId, rowIndex) {
	const tableBody = document.getElementById(tableId);
	const rows = tableBody.querySelectorAll('tr');

	// 清除所有行的选中状态
	rows.forEach(row => {
		row.classList.remove('selected');
	});

	// 检查索引是否有效
	if (rowIndex >= 0 && rowIndex < rows.length) {
		const targetRow = rows[rowIndex];
		targetRow.classList.add('selected');
		return targetRow;
	}

	return null;
}


// 选中并滚动到指定行
function selectAndScrollToRow(tableId, rowIndex) {
	const targetRow = selectRow(tableId, rowIndex);

	if (targetRow) {
		// 使用平滑滚动效果
		targetRow.scrollIntoView({
			behavior: 'smooth',
			block: 'center' // 将行滚动到容器中间
		});

		// 如果需要，可以添加焦点以便键盘导航
		targetRow.setAttribute('tabindex', '-1');
		targetRow.focus();
	}
}

function getTableCellText(tableId, rowIndex, colIndex) {
	const table = document.getElementById(tableId);
	const text = table.rows[rowIndex].cells[colIndex].textContent;
	return text;
}


function findRowInTbaleBodyByColumnValue(tableId, columnIndex, searchValue) {
	const table = document.getElementById(tableId);
	const tbody = table.tBodies[0]; // 获取第一个tbody
	const rows = tbody.rows;

	for (let i = 0; i < rows.length; i++) {
		const cell = rows[i].cells[columnIndex];
		if (cell && cell.textContent.trim() === searchValue) {
			return i; // 返回行索引(从0开始)
		}
	}
	return -1; // 未找到返回-1
}


function findRowInTableByColumnValue(tableId, columnIndex, searchValue) {
	const table = document.getElementById(tableId);
	const rows = table.rows;

	for (let i = 0; i < rows.length; i++) {
		const cell = rows[i].cells[columnIndex];
		if (cell && cell.textContent.trim() === searchValue) {
			return i; // 返回行索引(从0开始)
		}
	}
	return -1; // 未找到返回-1
}


// 添加行高亮效果
function setupRowHighlight(tableId) {
	const table = document.getElementById(tableId);
	const rows = table.querySelectorAll('tbody tr');

	rows.forEach(row => {
		row.addEventListener('click', function() {
			// 移除同表格中其他行的高亮
			debugger;
			this.closest('table').querySelectorAll('tr').forEach(r => {
				const cell = r.cells[0];
				var v = cell.textContent.trim();
				if (v == "") { //空行不处理
					return;
				}
				r.classList.remove('selected');
			});
			const cell = row.cells[0];
			var v = cell.textContent.trim();
			if (v == "") { //空行不处理
				return;
			}
			// 添加当前行高亮
			this.classList.add('selected');
		});
	});
}


function appendBlank(tabId, rows, cols) {
	const body = document.getElementById(tabId);
	if (body == null) {
		return;
	}
	var html = body.innerHTML;
	for (var i = 0; i < rows; i++) {
		html += "<tr>";
		for (var k = 0; k < cols; k++) {
			html += "<td>&nbsp;</td>"
		}
		html += "</tr>";
	}
	body.innerHTML = html;
}

function fatchCode(code) {
	var ca = code.split("/");
	if (ca.length == 0) {
		return ca;
	}
	return ca[0];
}
	
function search(code, tableId) {
	debugger;
	if (code == null) {
		return;
	}
	var ca = code.split("/");
	if (ca.length >= 0) {
		code = ca[0];
	}
	var row = findRowInTbaleBodyByColumnValue(tableId, 0, code);
	if (row >= 0) {
		selectAndScrollToRow(tableId, row + 1);  //thead作为第一行
		setCurrentCode(code);
	}
}

function showRow(rowId) {
   const row = document.getElementById(rowId);
   if(row == null){
	 return ;
   }
   row.style.display = 'table-row'; // 恢复 
 }

function hideRow(rowId) {
   const row = document.getElementById(rowId);
   if(row == null){
	 return ;
   }
   row.style.display = 'none'; // 隐藏 
 }
 
